<?php
if(!check_manager_login()) exit('无权查看，请登录!');
require_once(MODEL_PATH.DS.'Product.php');
require_once(MODEL_PATH.DS.'ProductDesc.php');
require_once(MODEL_PATH.DS.'ProductImage.php');
require_once(LIB_PATH.DS.'upload.class.php');
require_once(LIB_PATH.DS.'thumbnail.class.php');
$Product = new Product();
$ProductImage = new ProductImage();
$product_id = isset($_GET['product_id'])?(int)$_GET['product_id']:'';
if(check_post()){
	$image_ids = $_POST['imageid'];
	$old_image = explode(",",$_POST['oldimgids']);
	if(!$image_ids) $image_ids = array();
	$diff_ids = implode(",",array_diff($old_image, $image_ids));
	if($diff_ids){
		$del_img = $ProductImage->getProductImgByImageIds($diff_ids);
		if($del_img){
			foreach($del_img as $del){
				if(file_exists(PRODUCT_IMAGES.$del['image'])){
					@unlink(PRODUCT_IMAGES.$del['image']);
				}
				if(file_exists(PRODUCT_IMAGES.$del['thumbnail'])){
					@unlink(PRODUCT_IMAGES.$del['thumbnail']);
				}
			}
		}
		$result = $ProductImage->delProductImgByImageIds($diff_ids);
	}
	$Upload = new Upload('product_images/');
	$Upload->setMaxSize(1002400);
	$upload_status = $Upload->move();
	$messages = $Upload->getMessages();
	$product_id = $_POST['product_id'];
	$image_arr = $Upload->getFilename();
	if($image_arr){
		foreach($image_arr as $img){
			$Thumbnail = new Thumbnail(PRODUCT_IMAGES.$img);
			$Thumbnail->setDestination(PRODUCT_IMAGES);
			$Thumbnail->create();
			$thb_name = $Thumbnail->getFilename();
			$result = $ProductImage->addImage($product_id,$img,$thb_name);
		}
	}
}
$name_list = $Product->getAllProductName();
$product_select = create_select($name_list,'product_id','name',$product_id);
if($product_id){
	$img_arr = $ProductImage->getProductImgByPid($product_id);
	$img_ids = get_array($img_arr,'product_image_id','string');
}

include load_tpl('product_upload');
?>